package servlet.car;

import util.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/updatecars.do")
public class UpdateCarsNumServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        double oldsprice = Double.parseDouble(session.getAttribute("zj").toString());
        int oldnum = Integer.parseInt(session.getAttribute("sl").toString());
        double sprice = oldsprice/oldnum;
        int num = Integer.parseInt(request.getParameter("num"));
        int id = Integer.parseInt(request.getParameter("goods_id"));
        DbUtil db = new DbUtil();
        String sql = "update cars set num = "+num+" ,goods_price="+sprice+" where id = "+id+" ";
        System.out.println(sql);
        db.executeUpdate(sql);
        response.sendRedirect("GotoCarServlet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
